package com.sjy.ylxt.entity.basic;


import com.alibaba.fastjson2.JSONObject;
import com.sjy.ylxt.enums.ExceptionEnum;
import com.sjy.ylxt.interfaces.BaseErrorInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel
public class ResultBody<T> implements Serializable {
    @ApiModelProperty("响应码")
    private Integer code;
    @ApiModelProperty("响应信息")
    private String message;
    @ApiModelProperty("响应信息")
    private T data;
    public ResultBody() {
    }

    /**
     * 成功
     *
     * @return
     */
    public static <T> ResultBody<T> success() {
        return success(null);
    }


    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static <T> ResultBody<T> success(T data) {
        return new ResultBody<>(ExceptionEnum.SUCCESS, data);
    }

    /**
     * 失败
     */
    public static <T> ResultBody<T> error(BaseErrorInfo errorInfo) {
        return new ResultBody<>(errorInfo.getResultCode(), errorInfo.getResultMsg());
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    /**
     * 失败
     */
    public static <T> ResultBody<T> error(Integer code, String message) {
        return new ResultBody<>(code, message, null);
    }

    /**
     * 失败
     */
    public static <T> ResultBody<T> error(String message) {
        return new ResultBody<>(ExceptionEnum.ERROR.getResultCode(), message);
    }

    private <E extends BaseErrorInfo> ResultBody(E exceptionEnum) {
        this.code = exceptionEnum.getResultCode();
        this.message = exceptionEnum.getResultMsg();
        ;
    }

    private <E extends BaseErrorInfo> ResultBody(E exceptionEnum, T data) {
        this.code = exceptionEnum.getResultCode();
        this.data = data;
        this.message = exceptionEnum.getResultMsg();
        ;
    }

    private ResultBody(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    private ResultBody(Integer code, String msg, T data) {
        this.code = code;
        this.data = data;
        this.message = msg;
    }
}
